package com.mapforge.map.tools;

import java.awt.Point;

import com.mapforge.map.TileMap;
import com.mapforge.map.tools.undo.tilemapedit.ZoomEdit;

/** */
public class ZoomTool extends AbstractMapTool {
	/**
	 * Point where the mouse was released. Zoom will center onthis point
	 */
	private Point releasePoint;
	/**
	 * Indicates whether this tools edit should zoom in. If false it will
	 * decrease zoom instead.
	 */
	private boolean zoom_in;

	/** */
	public ZoomTool() {
		super("");
		zoom_in = true;
	}

	public void setDemagnify() {
		zoom_in = false;
	}

	public void setMagnify() {
		zoom_in = true;
	}

	/** */
	public String getDescription() {
		return "Zoom";
	}

	@Override
	public Point getMovingPoint() {
		return releasePoint;
	}

	@Override
	public Point getAnchorPoint() {
		return releasePoint;
	}

	@Override
	public void performEdit() {
		// intentionally left blank
	}

	@Override
	public void setMovingPoint(Point currentPoint) {
		this.releasePoint = currentPoint;
	}

	@Override
	public void setAnchorPoint(Point startPoint) {
		this.releasePoint = startPoint;
	}

	@Override
	public void endEdit() {
		super.endEdit();
		TileMap tiledMap = getTileMap();
		float oldScale = tiledMap.getScale();
		float scale = oldScale;
		if (this.zoom_in) {
			if (scale < 1) {
				scale *= 2;
				tiledMap.setScale(scale);
			} else {
				scale++;
				tiledMap.setScale((int) scale);
			}
		} else {
			if (scale <= 1) {
				scale /= 2;
				tiledMap.setScale(scale);
			} else {
				scale--;
				tiledMap.setScale((int) scale);
			}
		}
		ZoomEdit edit = new ZoomEdit(tiledMap, oldScale);
		this.fireEditActionPerformed(edit);
	}
}
